﻿using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using QbservableCore.Serialization.Helpers;
using System.Runtime.Serialization;

namespace QbservableCore.Serialization
{
    /// <summary>
    /// Serializable base class for all expression's type
    /// </summary>
    [DataContract]
    public abstract class ExpRepresentationBase
    {
        protected internal abstract Expression ToExpression(ExepressionBuilderCache exepressionBuilderCache);

        public ExpRepresentationBase Traverse(Func<ExpRepresentationBase, ExpRepresentationBase> action)
        {
            TraverseChildren(action);
            return action(this);
        }

        protected abstract void TraverseChildren(Func<ExpRepresentationBase, ExpRepresentationBase> action);

        protected static void TraverseNodes(ExpRepresentationBase[] nodes, Func<ExpRepresentationBase,ExpRepresentationBase> action)
        {
            for (int index = 0; index < nodes.Length; index++)
            {
                nodes[index] = nodes[index].Traverse(action);
            }
        }
    }
}